<?php
	session_start();
	include_once("connect.php");
	include_once("do_listing.php");
	include_once("post_model.php");
	mysql_select_db("progin_171_13509047",$con);
	include_once("header.php");
	
	$p_model = new Post_Model();
	$last = (isset($_GET['last']))? $_GET['last']:0;//untuk navigasi page
	$operation = (isset($_GET['operation']))?  $_GET['operation']:"view";//mengeset operasi apa yang harus dilakukan
	$tag = (isset($_GET['tag']))? $_GET['tag']:"";
	$filtermode = (isset($_GET['filtermode']))? $_GET['filtermode']:"";
	$sortmode = (isset($_GET['sortmode']))? $_GET['sortmode']:"";
	$viewer_id = (isset($_SESSION['userid']))? $_SESSION['userid']:"";
	/* $p_model->doOperation($operation); */
	
	$maxinpage=10;//maximum post yang ditampilkan di daftar kontent
?>
	<!-- Body -->
	<div id="bodycontent">
	  <div class="contentcontainer">
	    
		<div class="title-text">
		  <h1>Daftar konten</h1>
		</div>
		
		<div class = "contentbox">
			<form method="GET" action=list-content.php>
				<div class="form-element1">Filter By Tags(pisahkan dengan koma ',')</div>
				<div class="form-element2"><input type="text" name="tag"size="35"/></div>
				<br/>
				<div class="form-element1">Mode Filter</div>
				<div class="form-element2">
					<select name="filtermode">
						<option value="OR">or</option>
						<option value="AND">and</option>
						<option value="NOT">not</option>
					</select>
				</div>
				<br/>
				<div class="form-element1">Sorting Mode</div>
				<div class="form-element2">
					<select name="sortmode">
						<option value="datetime">Time</option>
						<option value="like_count">Likes</option>
						<option value="comment_count">Comments</option>
					</select>
				</div>
				<br/>
				<div class="form-element1"><input class="button" type="submit" value="Filter Now"/></div>
			</form>
		</div> <!-- end of contentbox untuk filter dan sorting-->
	<div class = "contentbox">
		<span>AllTags:</span>
		<div class= "input_tag">
		</div>
		<div class = "tag_list">
			<?php
				$p_model->showAllTags();
			?>
		</div>
	</div>
	<?php
		str_replace(" ","",$tag);
		$count_query = $p_model->generateCountQuery($tag,$filtermode,$sortmode,$last,$maxinpage);
		$query = $p_model->generateQuery($tag,$filtermode,$sortmode,$last,$maxinpage);
		/* generateQuery($tag,$filtermode,$sortmode,$last,$maxinpage); */
		if (!empty($count_query) && !empty($query))
		{
			$dbcontent_query = mysql_query($query) or die(mysql_error());
			$dbcount = mysql_query($count_query) or die(mysql_error());
			$datarow = mysql_num_rows($dbcount);
			$lastindex = convertQuery($dbcontent_query,$viewer_id);
		}
		else
		{?>
		<div class="contentbox">
			<span style="color:red;">Tag Kosong<span>
		</div>
<?php			
		}
	?>
	
	  </div>
	</div>
	<script type="text/javascript">
	function jlastobject()
	{
		this.value =  <?php echo $last?>;
	}
	var max=0;
	var jlast = new jlastobject();
	var jtag = <?php echo "\"$tag\""?>;
	var jfilter = <?php echo "\"$filtermode\""?>;
	var jsort = <?php echo "\"$sortmode\""?>;
	var jmaxinpage = <?php echo $maxinpage?>;
	var jmaxheight = new maxHeight(max);
	console.log(jmaxheight.value);
		setBodyDynamicScroll(jlast,jtag,jfilter,jsort,jmaxinpage,jmaxheight)
	</script>
<?php
	include_once("footer.php");
	include_once("close_con.php");
?>